/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package udp2serial;
import java.io.*;
import java.util.*;
import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;

/**
 *
 * @author axelsben
 */
public class tx {
    private static String useComport = "COM1";
    private static String useBaudrate = "9600";
    private static javax.swing.JTextArea jta = null;
    private static OutputStream useTx = null;
    private static boolean logging = false;

    public static void setLogging(boolean bool)
    {
        logging = bool;
    }


    public static void setTA(javax.swing.JTextArea jtxta)
    {
        jta = jtxta;
    }

    public static void setComport(String comport)
    {
        useComport = comport;
    }

    public static void setBaudrate(String baudrate)
    {
        useBaudrate = baudrate;
    }

    public static String getComport()
    {
        return useComport;
    }

    public static String getBaudrate()
    {
        return useBaudrate;
    }

    public static void setOutputStrem(OutputStream theTX)
    {
        useTx = theTX;
    }

    public static void send(String[] args) throws IOException {
        if(useTx != null)
        {
            for(int ix=0 ; ix < args.length; ix++)
            {
                useTx.write(args[ix].getBytes());
                if(logging)
                {
                    jta.setSelectionStart(jta.getText().length());
                    jta.setSelectionEnd(jta.getText().length()+1);
                    jta.replaceSelection("/st:" + args[ix]);
                    if(jta.getText().length() > 25000)
                    {
                        int rm = jta.getText().length() - 25000;
                        jta.setSelectionStart(0);
                        jta.setSelectionEnd(rm);
                        jta.replaceSelection("");
                    }
                    jta.setSelectionStart(jta.getText().length());
                    jta.setSelectionEnd(jta.getText().length()+1);
                }
                //String s = jta.getText();
                //jta.setText("|tx->Ser: " + args[0] + s + "\n");
            }
        }
    }

}
